package com.wmh.baseservice.operationaid.controller.store;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.operationaid.entity.TbStoreData;
import com.wmh.baseservice.operationaid.pojo.vo.store.TbStoreListReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.store.TbStoreOneDayComparisonReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.store.TbStorePageReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.store.TbStoreSameDateComparisonReqVO;
import com.wmh.baseservice.operationaid.service.TbStoreDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

@Validated
@RestController
@RequestMapping("store/tb")
@Api(tags = "店铺数据-淘宝系")
@Slf4j
public class TbStoreController {
    @Resource
    private TbStoreDataService tbStoreDataService;

    @ApiOperation(value = "批量删除天猫店铺列表")
    @PostMapping("/del")
    public String batchRemove(@Size(min = 3, message = "请上传主键") @NotBlank(message = "请上传主键") String ids) {
        tbStoreDataService.batchRemove(JSON.parseArray(ids, String.class));
        return ReturnBody.success();
    }

    @ApiOperation(value = "获取列表")
    @PostMapping("/list")
    public String list(TbStoreListReqVO reqVO){
        return success(tbStoreDataService.getList(reqVO), JSONWriter.Feature.NullAsDefaultValue);
    }

    @ApiOperation(value = "获取淘宝店铺分页列表")
    @PostMapping("/page")
    public String page(@Validated TbStorePageReqVO reqVO){

        PageResult<TbStoreData> pageResult = new PageUtil<TbStoreData>(reqVO).startPage((page, wrapper) -> {

            wrapper.eq(StrUtil.isNotBlank(reqVO.getStoreName()), TbStoreData.COL_STORE_NAME , reqVO.getStoreName());

            if (StrUtil.isAllNotBlank(reqVO.getStartDate(), reqVO.getEndDate())) {
                wrapper.between(TbStoreData.COL_DATA_DATE,
                        LocalDateTimeUtil.parse(reqVO.getStartDate() + "T00:00:00"),
                        LocalDateTimeUtil.parse(reqVO.getEndDate() + "T00:00:00"));
            }

            tbStoreDataService.page(page, wrapper);
        });
        return success(pageResult);
    }

    @ApiOperation(value = "单日数据对比")
    @PostMapping("oneDayComparison")
    public String oneDayComparison(@Valid TbStoreOneDayComparisonReqVO reqVO){
        return success(tbStoreDataService.oneDayComparison(reqVO), JSONWriter.Feature.NullAsDefaultValue);
    }

    @ApiOperation(value = "周期数据对比")
    @PostMapping("sameDateComparison")
    public String sameDateComparison(@Valid TbStoreSameDateComparisonReqVO reqVO){
        return success(tbStoreDataService.sameDateComparison(reqVO), JSONWriter.Feature.WriteNulls);
    }

    @ApiOperation("获取店铺选项列表")
    @PostMapping("storeOptions")
    public String getStoreOptions(){
        return success(tbStoreDataService.getStoreOptions());
    }

    @ApiOperation("获取数据日期选项")
    @PostMapping("dataDateOptions")
    public String dataDateOptions(){
        return success(tbStoreDataService.getDataDateOptions());
    }


}
